from . import ConnPG
import psycopg2
import sys
from sanic.log import logger


class DutyWarehouseTable:

    # 添加一个关系
    async def add_ship(self, duty_id, warehouse_id):  # 添加关系

        # 参数检查


        if not all([duty_id, warehouse_id]):

            return {
                'status': -1,
                'errorCode': 333100,
                'errorMsg': '检查参数',
                'resultBody': None
            }

        sql = '''\
        INSERT INTO duty_warehouse(
            duty_id, warehouse_id
            )
        VALUES (
            %s, %s) RETURNING id'''

        try:
            result = await ConnPG().fetchone(
                sql,
                (duty_id, warehouse_id)

            )
        except psycopg2.errors.UniqueViolation as e:
            print(sys.exc_info())


            return {
                'status': -1,
                'errorCode': 333101,
                'errorMsg': str(e),
                'resultBody': None,
            }
        else:

            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': result,
            }

    # 更改权限
    async def updte_ship(self, duty_id, warehouse_id, 
                            out, in_ ,out_odd, in_odd, in_operation, out_operation, examine):

        # print([out, in_ ,out_odd, in_odd, examine])

        if not all([duty_id, warehouse_id]):

            return {
                'status': -1,
                'errorCode': 333100,
                'errorMsg': '检查参数',
                'resultBody': None
            }

        sql = '''
        UPDATE 
            duty_warehouse 
        SET 
            out = %s,
            in_ = %s,
            out_odd = %s,
            in_odd = %s,
            in_operation = %s,
            out_operation = %s,
            examine = %s
        WHERE 
            duty_id = %s
            AND
            warehouse_id = %s

        '''

        await ConnPG().execute(sql,(out, in_ ,out_odd, in_odd,  in_operation, out_operation,
                                    examine,duty_id, warehouse_id))


        return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': '更新成功',
            }

    

    async def select_all_ship(self):#查询所有的关系

        sql = "select * from view_duty_warehouse"

        result = await ConnPG().fetchall(sql)
        #print(result)
        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    async def del_ship(self, duty_id, warehouse_id):#查询所有的关系

        if not all([duty_id, warehouse_id]):

            return {
                'status': -1,
                'errorCode': 333100,
                'errorMsg': '检查参数',
                'resultBody': None
            }

        # sql = "select * from duty_warehouse"
        sql = "delete from duty_warehouse where duty_id=%s and warehouse_id=%s"


        await ConnPG().execute(sql, (duty_id, warehouse_id))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': '删除成功',
        }

    
